package com.zx.mes.hyl.auth.rest;

import com.zx.mes.hyl.auth.pagemodel.UserInfo;
import com.zx.mes.hyl.auth.service.UserServiceI;
import com.zx.mes.hyl.common.exception.auth.UserInvalidException;
import com.zx.mes.hyl.common.msg.ObjectRestResponse;
import com.zx.mes.hyl.common.util.jwt.JWTHelper;
import com.zx.mes.hyl.common.util.jwt.JWTInfoI;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * JWT
 * @author hyl
 * @date 2017-12-17
 */

@RestController
@RequestMapping("jwt")
public class JWTController {

    @Value("${com.jwt.expire}")
    private Integer expire;

    @Value("${com.jwt.pub-key.path}")
    private String pubKeyPath;

    @Value("${com.jwt.pri-key.path}")
    private String priKeyPath;

    @Value("${com.sam.name}")
    private String namess;

    private Logger logger=Logger.getLogger(JWTController.class);

    @Autowired
    private JWTHelper jwtHelper;

    @Autowired
    private UserServiceI userService;

    @RequestMapping("/token")
    public ObjectRestResponse<String> authorize(String username, String password){
        logger.info("key:auth-123456789"+"用户名:"+username+" 密码:"+password);

        ObjectRestResponse<String> restResponse=new ObjectRestResponse<String>();
        //1.校验帐户合法性
        UserInfo userInfo=userService.login(username,password);
        if (userInfo==null){
            throw new UserInvalidException("非法用户");
        }
        //2.合法则生成token
        try {
            UserInfo user=new UserInfo();
            restResponse.setData(jwtHelper.generateToken(userInfo,priKeyPath,expire));
            restResponse.setRel(true);
            logger.info("key:auth-123456789"+"token:"+restResponse.getData());
        } catch (Exception e) {
            restResponse.setRel(false);
            restResponse.setStatus(500);
            restResponse.setMessage("异常:"+e.getMessage());
            e.printStackTrace();
        }

        return restResponse;
    }

    @RequestMapping("/userInfo")
    @ResponseBody
    public ObjectRestResponse<JWTInfoI> getUserInfo(String token){
        ObjectRestResponse<JWTInfoI> response=new ObjectRestResponse<>();
        JWTHelper jwtHelper=new JWTHelper();
        JWTInfoI info;
        UserInfo userInfo=new UserInfo();

        try {
            info=jwtHelper.getInfoFromToken(token,pubKeyPath);
            userInfo.setResource(userService.resourceList(info.getId()));
            userInfo.setMenu(userService.menuList(info.getId()));
            userInfo.setUsername(info.getUniqueName());
            response.setStatus(200);
            response.setRel(true);
        } catch (Exception e) {
            response.setStatus(500);
            response.setRel(false);
            response.setMessage("异常:"+e.getMessage());
            info=null;
            e.printStackTrace();

        }
        response.setData(userInfo);
        return response;
    }

    @RequestMapping("/logout")
    public ObjectRestResponse<String> logout(){
        return new ObjectRestResponse<>();
    }
}
